import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UnitsProvider } from '../../providers/units/units';
/**
 * Generated class for the Resourceslist page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
	selector: 'page-resourceslist',
	templateUrl: 'resourceslist.html',
})
export class ResourceslistPage {

	private selectIndex: any;
	private listItemsTodoPage: any;
	private listItemsAlreadyPage: any;
	/**
	 * 列表
	 */
	private listItemsTodo: Array < {
		taskId: any,
		taskName: any,
		procDefId: any,
		procInstId: any,
		businessId: any,
		title: any,
		applyUserId: any,
		taskCreateDate: any
	} > ;
	private listItemsAlready: Array < {
		taskId: any,
		taskName: any,
		procDefId: any,
		procInstId: any,
		businessId: any,
		title: any,
		applyUserId: any,
		taskCreateDate: any
	} > ;
	/**
	 * 选择框
	 */
	private inputSearch = {
		procDefKey: new Object(),
		assignee: new Object(),
		beginDate: '',
		endDate: ''
	};

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public units: UnitsProvider) {
		this.listItemsTodo = new Array();
		this.listItemsAlready = new Array();
		this.selectIndex = this.navParams.get("type");
		this.listItemsTodoPage = 1;
		this.listItemsAlreadyPage = 1;
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad Resourceslist');
		this.loadData("-1");
	}
	//初始化
	loadData(index: any) {
		//异步方式
		return new Promise((success) => {
			this.storage.getStorage("user").then(val => {
				if(index == "0" || index == "-1") {
					this.listItemsTodoPage = 1;
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItemsTodoPage,
						procDefKey: this.inputSearch.procDefKey['id'] == undefined ? "" : this.inputSearch.procDefKey['id'],
						assignee: this.inputSearch.assignee['id'] == undefined ? "" : this.inputSearch.assignee['id'],
						beginDate: this.inputSearch.beginDate,
						endDate: this.inputSearch.endDate
					};
					//收文传阅列表
					this.http.postJson('hr', 'toDoList.do', params).then(res => {
						if(res['data'] != undefined) {
							let datas = res['data']['list'];
							this.loadMakeData("0", true, datas);
							if(success)
								success();
						} else {
							if(success)
								success();
						}
					});
				}
				if(index == "1" || index == "-1") {
					this.listItemsAlreadyPage = 1;
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItemsAlreadyPage,
						procDefKey: this.inputSearch.procDefKey['id'] == undefined ? "" : this.inputSearch.procDefKey['id'],
						assignee: this.inputSearch.assignee['id'] == undefined ? "" : this.inputSearch.assignee['id'],
						beginDate: this.inputSearch.beginDate,
						endDate: this.inputSearch.endDate
					};
					//收文传阅列表
					this.http.postJson('hr', 'doneList.do', params).then(res => {
						if(res['data'] != undefined) {
							let datas = res['data']['list'];
							this.loadMakeData("1", true, datas);
							if(success)
								success();
						} else {
							if(success)
								success();
						}
					});
				}
			});
		});
	}
	//加载更多
	loadMoreData(index: any) {
		//异步方式
		return new Promise((success) => {
			this.storage.getStorage("user").then(val => {
				if(index == "0") {
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItemsTodoPage + 1,
						procDefKey: this.inputSearch.procDefKey['id'] == undefined ? "" : this.inputSearch.procDefKey['id'],
						assignee: this.inputSearch.assignee['id'] == undefined ? "" : this.inputSearch.assignee['id'],
						beginDate: this.inputSearch.beginDate,
						endDate: this.inputSearch.endDate
					};
					//收文传阅列表
					this.http.postJson('hr', 'toDoList.do', params).then(res => {
						if(res['data'] != undefined) {
							let datas = res['data']['list'];
							this.loadMakeData(index, false, datas);
							if(datas.length > 0)
								this.listItemsTodoPage++;
							if(success)
								success();
						} else {
							if(success)
								success();
						}
					});
				} else {
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItemsAlreadyPage + 1,
						procDefKey: this.inputSearch.procDefKey['id'] == undefined ? "" : this.inputSearch.procDefKey['id'],
						assignee: this.inputSearch.assignee['id'] == undefined ? "" : this.inputSearch.assignee['id'],
						beginDate: this.inputSearch.beginDate,
						endDate: this.inputSearch.endDate
					};
					//收文传阅列表
					this.http.postJson('hr', 'doneList.do', params).then(res => {
						if(res['data'] != undefined) {
							let datas = res['data']['list'];
							this.loadMakeData(index, false, datas);
							if(datas.length > 0)
								this.listItemsTodoPage++;
							if(success)
								success();
						} else {
							if(success)
								success();
						}
					});
				}
			});
		});
	}
	//共通列表
	loadMakeData(index: any, isnew: any, datas: any) {
		if(index == "0") {
			if(isnew)
				this.listItemsTodo = new Array();
			for(let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.listItemsTodo.push({
					taskId: item['taskId'],
					taskName: item['taskName'],
					procDefId: item['procDefId'],
					procInstId: item['procInstId'],
					businessId: item['businessId'],
					title: item['title'],
					applyUserId: item['applyUserId'],
					taskCreateDate: item['taskCreateDate']
				});
			}
		} else {
			if(isnew)
				this.listItemsAlready = new Array();
			for(let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.listItemsAlready.push({
					taskId: 0,
					taskName: item['name'],
					procDefId: item['procDefId'],
					procInstId: item['procInstId'],
					businessId: item['businessId'],
					title: item['title'],
					applyUserId: item['startUserId'],
					taskCreateDate: this.units.getIntToDateString(item['startTime'])
				});
			}
		}
	}

	//搜索
	dosearch() {
		let data = new Object();
		let params = new Object();
		params['applyapplycode'] = this.inputSearch.procDefKey;
		params['applyapplymember'] = this.inputSearch.assignee;
		params['applystarttime'] = this.inputSearch.beginDate;
		params['applyendtime'] = this.inputSearch.endDate;
		data['type'] = 1; //检索类型
		data['params'] = params; //参数传递
		let opts = new Object();
		this.dialog.showModal("ModalsearchPage", data, opts).then(success => {
			//界面关闭的时候
			if(success) {
				this.inputSearch.procDefKey = success['applyapplycode'];
				this.inputSearch.assignee = success['applyapplymember'];
				this.inputSearch.beginDate = success['applystarttime'];
				this.inputSearch.endDate = success['applyendtime'];
				this.loadData(this.selectIndex);
			}
		});
	}

	/*下拉加载*/
	doRefresh(refresher) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			/*判断是哪个列表显示*/
			this.loadData(this.selectIndex).then(success => {
				refresher.complete();
			});
		}, 1500);
	}
	/*上拉加载*/
	doInfinite(infiniteScroll) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			this.loadMoreData(this.selectIndex).then(success => {
				infiniteScroll.complete();
			});
		}, 1500);
	}
	//详情
	dodetail(item: any) {

	}

}